package com.cf.base.sp;

import android.content.Context;

import com.cf.base.sp.annotation.Level;
import com.cf.base.sp.builder.IBuilder;
import com.cf.base.sp.engine.impl.DefaultProcessorImpl;
import com.cf.base.sp.engine.Processor;

/**
 * SpCore 对外提供接口
 *
 * @author yangji
 */
public class SpCore {

    private static Processor mProcessor;

    private SpCore() {
    }

    public static IBuilder init(Context context) {
        return new SpCoreBuilder(context);
    }

    static void build(SpCoreBuilder build) {
        mProcessor = new DefaultProcessorImpl(build);
    }

    //********************** put **********************

    public static <T> boolean put(String key, T value) {
        return putCache(key, value);
    }

    public static <T> boolean putCache(String key, T value) {
        return put(Level.Cache, key, value);
    }

    public static <T> boolean putUser(String key, T value) {
        return put(Level.User, key, value);
    }

    public static <T> boolean putSystem(String key, T value) {
        return put(Level.System, key, value);
    }

    public static <T> boolean put(Level level, String key, T value) {
        return mProcessor.put(key, value, level);
    }

    //********************** get **********************

    public static <T> T get(String key) {
        return getCache(key);
    }

    public static <T> T get(String key, T defaultValue) {
        return getCache(key, defaultValue);
    }

    public static <T> T getCache(String key) {
        return get(Level.Cache, key);
    }

    public static <T> T getCache(String key, T defaultValue) {
        return get(Level.Cache, key, defaultValue);
    }

    public static <T> T getUser(String key) {
        return get(Level.User, key);
    }

    public static <T> T getUser(String key, T defaultValue) {
        return get(Level.User, key, defaultValue);
    }

    public static <T> T getSystem(String key) {
        return get(Level.System, key);
    }

    public static <T> T getSystem(String key, T defaultValue) {
        return get(Level.System, key, defaultValue);
    }

    public static <T> T get(Level level, String key) {
        return get(level, key, null);
    }

    public static <T> T get(Level level, String key, T defaultValue) {
        T t = mProcessor.get(key, level);
        if (t == null) {
            return defaultValue;
        }
        return t;
    }


    //********************** count **********************

    public static int countCache() {
        return mProcessor.count(Level.Cache);
    }

    public static int countUser() {
        return mProcessor.count(Level.User);
    }

    public static int countSystem() {
        return mProcessor.count(Level.System);
    }

    public static int count(Level level) {
        return mProcessor.count(level);
    }


    //********************** contains **********************

    public static boolean contains(String key) {
        return containsCache(key);
    }

    public static boolean containsCache(String key) {
        return contains(Level.Cache, key);
    }

    public static boolean containsUser(String key) {
        return contains(Level.User, key);
    }

    public static boolean containsSystem(String key) {
        return contains(Level.System, key);
    }

    public static boolean contains(Level level, String key) {
        return mProcessor.contains(key, level);
    }

    //********************** clear **********************

    public static void clearCache() {
        clear(Level.Cache);
    }

    public static void clearUser() {
        clear(Level.User);
    }

    public static void clear(Level level) {
        if (level == Level.System) {
            return;
        }
        mProcessor.clear(level);
    }

    //********************** remove **********************

    public static void remove(String key) {
        removeCache(key);
    }

    public static void removeCache(String key) {
        remove(Level.Cache, key);
    }

    public static void removeUser(String key) {
        remove(Level.User, key);
    }

    public static void removeSystem(String key) {
        remove(Level.System, key);
    }

    public static void remove(Level level, String key) {
        mProcessor.delete(key, level);
    }


    //********************** onDestroy **********************

    public static void onDestroy() {
        //因为只有这里是单例 其他的地方都是对象 所以不用过于担心内存泄漏
        mProcessor = null;
    }

}
